package  
{
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	/**
	 * ...
	 * @author Matthew Shaffer
	 */
	public class Board extends Sprite
	{
		private var rows: int = 50;
		private var columns: int = 50;
		private var board: Array;
		private var unitSize: int = 12;
		private var nonSelectedColor: uint = 0x000000;
		private var selectedColor: uint = 0xffffff;
		private var selectedPair: Array = new Array (0,0);
		
		
		public function Board() 
		{
			board = new Array (rows);
			for (var i:int = 0; i < board.length; i++ ) {
				board[i] = new Array (columns);
				for (var j:int = 0; j < board[i].length; j++ ) {
					board[i][j] = new Unit();
				}
			}
			board[selectedPair[0]][selectedPair[1]].setSelected();
			DrawGraphics();
			this.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
		}
		
		public function keyDownHandler (event:KeyboardEvent) : void {
			if ((selectedPair[0] > 0) && (event.keyCode == 37)) {
				board[selectedPair[0]][selectedPair[1]].setNotSelected();
				board[selectedPair[0] - 1][selectedPair[1]].setSelected();
				selectedPair[0] = selectedPair[0] - 1;
			} else if ((selectedPair[0] < board.length-1) && (event.keyCode == 39)) {
				board[selectedPair[0]][selectedPair[1]].setNotSelected();
				board[selectedPair[0] + 1][selectedPair[1]].setSelected();
				selectedPair[0] = selectedPair[0] + 1;
			} else if ((selectedPair[1] > 0) && (event.keyCode == 38)) {
				board[selectedPair[0]][selectedPair[1]].setNotSelected();
				board[selectedPair[0]][selectedPair[1]-1].setSelected();
				selectedPair[1] = selectedPair[1] - 1;
			} else if ((selectedPair[1] < board[selectedPair[0]].length-1) && (event.keyCode == 40)) {
				board[selectedPair[0]][selectedPair[1]].setNotSelected();
				board[selectedPair[0]][selectedPair[1]+1].setSelected();
				selectedPair[1] = selectedPair[1] + 1;
			}else {
				selectedPair = selectedPair;
			}
			DrawGraphics();
		}
		
		public function DrawGraphics():void {
			this.graphics.clear();
			for (var i:int = 0; i < board.length; i++ ) {
				for (var j:int = 0; j < board[i].length; j++ ) {
					if (board[i][j].getSelected() == false) {
						this.graphics.lineStyle(1, nonSelectedColor);
					}else {
						this.graphics.lineStyle(1, selectedColor);
					}
					this.graphics.beginFill(0x00ff00);
					this.graphics.drawRect(i * unitSize, j * unitSize, unitSize-1, unitSize-1);
				}
			}
		}
		
	}
}